package Springboot.service.impl;

import Springboot.Utils.JwtHelper;
import Springboot.Utils.MD5Util;
import Springboot.Utils.Result;
import Springboot.Utils.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import Springboot.domain.NewsUser;
import Springboot.service.NewsUserService;
import Springboot.mapper.NewsUserMapper;
import org.graalvm.nativeimage.c.struct.AllowWideningCast;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author mazhenxi
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2024-09-03 17:51:37
*/
@Service
public class NewsUserServiceImpl extends ServiceImpl<NewsUserMapper, NewsUser>
    implements NewsUserService{
    @Autowired
    NewsUserMapper newsUserMapper;
    @Autowired
    JwtHelper jwtHelper;
    @Override
    public Result login(NewsUser user) {
        LambdaQueryWrapper<NewsUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NewsUser::getUsername,user.getUsername());
        NewsUser newsUser = newsUserMapper.selectOne(wrapper);
        //Result<NewsUser> result = new Result<>();
        if(newsUser==null) {
            return Result.build(null,501,"用户名有误");
        }
        String psd= MD5Util.encrypt(user.getUserPwd());
        if(psd.equals(newsUser.getUserPwd())) {
            String token = jwtHelper.createToken(Long.valueOf(newsUser.getUid()));
            Map objectObjectHashMap = new HashMap();//用map来存放token
            objectObjectHashMap.put("token",token);
            return Result.ok(objectObjectHashMap);//登录成功返回token用ok的方法
        }
        return Result.build(null,503,"密码有误");
    }

    @Override
    public Result getUserInfo(String token) {
        boolean loginFlag = jwtHelper.isExpiration(token);
        if (loginFlag) {
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        int id = jwtHelper.getUserId(token).intValue();
        NewsUser newsUser = newsUserMapper.selectById(id);
        if(newsUser!=null) {
            newsUser.setUserPwd(null);
            Map map=new HashMap();
            map.put("loginUser",newsUser);
            return Result.ok(map);
        }
    return Result.build(null,ResultCodeEnum.NOTLOGIN);
    }

    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<NewsUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<NewsUser> wrapper = lambdaQueryWrapper.eq(NewsUser::getUsername, username);
        NewsUser newsUser = newsUserMapper.selectOne(wrapper);
        if(newsUser!=null) {
            return Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        }
        return Result.ok(null);
    }

    @Override
    public Result regist(NewsUser newsUser) {
        LambdaQueryWrapper<NewsUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        LambdaQueryWrapper<NewsUser> wrapper = lambdaQueryWrapper.eq(NewsUser::getUsername, newsUser.getUsername());
        NewsUser loginUser = newsUserMapper.selectOne(wrapper);
        if(loginUser==null&& !newsUser.getUsername().isEmpty()) {
            newsUser.setUserPwd(MD5Util.encrypt(newsUser.getUserPwd()));
            newsUserMapper.insert(newsUser);

            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    @Override
    public Result checkLogin(String token) {
        boolean expiration = jwtHelper.isExpiration(token);
        if(expiration||token.isEmpty()) {
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}




